using System;
using System.Runtime.InteropServices;
using Microsoft.Windows.SyncManager.Interop;
using System.Drawing;
using System.Diagnostics;

namespace iPodShuffle
{
	[ComVisible(true)]
	[Guid("99DA05BF-8494-43da-BCE2-75EF736F365A")]
	public class FileSyncMgr: SyncMgrSynchronizeBase
	{
		#region override

		public override void GetHandlerInfo(out SYNCMGRHANDLERINFO info)
		{
			try
			{
				Icon icon        = GetItemIcon();

				info             = new SYNCMGRHANDLERINFO();
				info.hIcon       = icon.Handle;
				info.HandlerName = "iPod Shuffle Sync";
			}
			catch (Exception ex)
			{
				LogError(ex);
				throw;
			}	
		}

		public override void LogError( string text )
		{
			Debug.WriteLine( text );
			base.LogError( text );
		}
		public override void LogError( Exception error )
		{
			Debug.WriteLine( error );
			base.LogError( error.Message );
		}

		public override SYNCMGRITEM[] CreateItems()
		{
			string        drives = iPodShuffleHelper.LocateDrives();
			SYNCMGRITEM[] items  = CreateItems( drives );

			return items;
		}

		public override void Synchronize(SYNCMGRITEM item, IntPtr parentWindow)
		{
			// this can be run from another thread 

			string name  = item.ItemName;
			char   drive = name[name.Length-1];

			NotifyProgress( item.ItemID, "Collecting files from drive "+drive );

			string[] filesInDrive = iPodShuffleHelper.GetSoundFiles( drive );

			NotifyProgress( item.ItemID, string.Format("Found {0} files.", filesInDrive.Length ) );

foreach (string s in filesInDrive) Debug.WriteLine( s );

			string iTunesSD = drive+@":\iPod_Control\iTunes\iTunesSD";
			iPodShuffleHelper.Create_iTunesSD( iTunesSD, filesInDrive );

			NotifyProgress( 
				item.ItemID
				, string.Format("Play list updated with {0} files.", filesInDrive.Length )
				, SYNCMGRSTATUS.SUCCEEDED 
			);
		}

		#endregion

		#region helper

		SYNCMGRITEM[] CreateItems( string drives )
		{
			SYNCMGRITEM[] items = new SYNCMGRITEM[drives.Length];
			for(int i=0; i<items.Length; i++)
			{
				char drive = drives[i];

				// detect if the item should be checked/selected
				bool bCheck   = true;
				try
				{
					string[] files1 = iPodShuffleHelper.Read_iTunesSD( drive );
					string[] files2 = iPodShuffleHelper.GetSoundFiles( drive );
					bool     bEqual = iPodShuffleHelper.CompareSoundFiles( files1, files2 );

					bCheck = false == bEqual;
				}
				catch {}

				string   name = "iPod Shuffle in Disk: "+drive;
				DateTime time = iPodShuffleHelper.GetLastUpdateTime( drive );

				SYNCMGRITEM item = CreateItem( name, bCheck, time );
				items[i] = item;
			}

			return items;
		}

		SYNCMGRITEM CreateItem(string name, bool check, DateTime lastUpdateTime)
		{
			Icon icon  = GetItemIcon();

			SYNCMGRITEM item = new SYNCMGRITEM();
			item.cbSize		= Marshal.SizeOf( typeof(SYNCMGRITEM) );
			item.ItemID		= Guid.NewGuid();
			item.ItemName	= name;
			item.Flags		= SYNCMGRITEMFLAGS.LASTUPDATETIME;
			item.ItemState	= check? SYNCMGRITEMSTATE.CHECKED: SYNCMGRITEMSTATE.UNCHECKED;
			item.hIcon		= icon.Handle;
			item.LastUpdate	= iPodShuffleHelper.GetFileTime( lastUpdateTime );

			return item;
		}

		Icon m_ItemIcon;
		Icon GetItemIcon()
		{
			if ( null == m_ItemIcon)
				m_ItemIcon = new Icon( this.GetType(), "Icon1.ico" );

			return m_ItemIcon;		
		}

		#endregion
	}
}
